<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\api\home;

use think\Controller;
use think\Db;

/**
 * 邮件控制器
 * @package app\index\controller
 */
class Email extends Controller
{

    var $email_config;
	public function _initialize(){
        parent::_initialize();
		set_time_limit(0);
        $this->Configobj = Db::name("admin_config");
        $ConfigData=$this->Configobj->where(['group'=>'email','status'=>1])->order(array('id'=>'desc'))->select();
        foreach ($ConfigData as $r) {
            $this->email_config[$r['name']] = $r['value'];
        }
    }
    /**
     * 根据用户输入的Email跳转到相应的电子邮箱首页
     * @param String $mail  邮箱地址
     * @return String
     */
    public function gotomail($mail) {
        $t = explode('@', $mail);
        $t = strtolower($t[1]);
        if ($t == '163.com') {
            return 'http://mail.163.com';
        } else if ($t == 'vip.163.com') {
            return 'http://vip.163.com';
        } else if ($t == '126.com') {
            return 'http://mail.126.com';
        } else if ($t == 'qq.com' || $t == 'vip.qq.com' || $t == 'foxmail.com') {
            return 'http://mail.qq.com';
        } else if ($t == 'gmail.com') {
            return 'http://mail.google.com';
        } else if ($t == 'sohu.com') {
            return 'http://mail.sohu.com';
        } else if ($t == 'tom.com') {
            return 'http://mail.tom.com';
        } else if ($t == 'vip.sina.com') {
            return 'http://vip.sina.com';
        } else if ($t == 'sina.com.cn' || $t == 'sina.com') {
            return 'http://mail.sina.com.cn';
        } else if ($t == 'tom.com') {
            return 'http://mail.tom.com';
        } else if ($t == 'yahoo.com.cn' || $t == 'yahoo.cn') {
            return 'http://mail.cn.yahoo.com';
        } else if ($t == 'tom.com') {
            return 'http://mail.tom.com';
        } else if ($t == 'yeah.net') {
            return 'http://www.yeah.net';
        } else if ($t == '21cn.com') {
            return 'http://mail.21cn.com';
        } else if ($t == 'hotmail.com') {
            return 'http://www.hotmail.com';
        } else if ($t == 'sogou.com') {
            return 'http://mail.sogou.com';
        } else if ($t == '188.com') {
            return 'http://www.188.com';
        } else if ($t == '139.com') {
            return 'http://mail.10086.cn';
        } else if ($t == '189.cn') {
            return 'http://webmail15.189.cn/webmail';
        } else if ($t == 'wo.com.cn') {
            return 'http://mail.wo.com.cn/smsmail';
        } else {
            return "http://mail." . $t;
        }
    }
    public function test(){
        $ret = '{"email":"741459065@qq.com","content":"您的验证码：888888。如需帮助请联系客服。"}';
        $statuscode=self::sendemailapi($ret);
        echo $statuscode;
    }
    public function sendemailapi($ret){
        $ret=json_decode($ret,true);
        $content=trim($ret['content']);
        $statuscode=self::send_mail($ret['email'], "喜宝", $ret['subject'], $content);
        return $statuscode;
    }
    public function getemailsubject($varname,$parm=array()){
        $content=Db::name('admin_config')->where(array('group'=>'email','name'=>$varname))->value("value");
        foreach ($parm as $key => $value)
        {
            $content=str_replace("#".$key."#",$value,$content);
        }
        return $content;
    }
    public function getemailtemplate($varname,$parm=array()){
        $content=Db::name('admin_config')->where(array('group'=>'email','name'=>$varname))->value("value");
        foreach ($parm as $key => $value)
        {
            $content=str_replace("#".$key."#",$value,$content);
        }
        return $content;
    }
    /**
     * 系统邮件发送函数
     * @param string $to    接收邮件者邮箱
     * @param string $name  接收邮件者名称
     * @param string $subject 邮件主题
     * @param string $body    邮件内容
     * @param string $attachment 附件列表
     * @return boolean
     */
    public function send_mail($to, $name, $subject = '', $body = '', $attachment = null) {
        import("Vendor.phpmailer.phpmailer");
        $mail = new \PHPMailer();
        $mail->CharSet = 'UTF-8';
        $mail->IsSMTP();
        $mail->SMTPDebug = false;
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = 'ssl';
        $mail->Host = $this->mailconfig['mail_server'];
        $mail->Port = $this->mailconfig['mail_port'];
        $mail->Username = $this->mailconfig['mail_user'];
        $mail->Password = $this->mailconfig['mail_password'];
        $mail->SetFrom($this->mailconfig['mail_from'], $this->mailconfig['mail_fname']);
        $replyEmail = $this->mailconfig['mail_from'];
        $replyName = $this->mailconfig['mail_fname'];
        $mail->AddReplyTo($replyEmail, $replyName);
        $mail->Subject = $subject;
        $mail->MsgHTML($body);
        $mail->AddAddress($to, $name);
        if (is_array($attachment)) {
            foreach ($attachment as $file) {
                is_file($file) && $mail->AddAttachment($file);
            }
        }else{
            is_file($attachment) && $mail->AddAttachment($attachment);
        }
        return $mail->Send() ? true : $mail->ErrorInfo;
    }


}
